<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="提现单号" prop="withdrawalNum">
            <el-input v-model="queryParams.withdrawalNum" placeholder="请输入提现单号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="收款方信息" prop="payeeInfo">
            <el-input v-model="queryParams.payeeInfo" placeholder="请输入收款方信息" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="提现时间" style="width: 308px">
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['retail:withdrawalRecord:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="withdrawalRecordList" class="tableEmpty">
        <el-table-column label="提现单号" align="center" prop="withdrawalNum" />
        <el-table-column label="提现方式" align="center" prop="withdrawalType" />
        <el-table-column label="用户名" align="center" prop="userName" />
        <el-table-column label="账户ID" align="center" prop="accountId">
          <template #default="scope">
            <el-link @click="">{{ scope.row.accountId }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="amount">
          <template #default="scope">
            <el-link type="danger">{{ scope.row.amount/100 }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="手续费" align="center" prop="fee">
          <template #default="scope">
            <el-link type="primary">{{ scope.row.fee/100 }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="收款方信息" align="center" prop="payeeInfo" />
        <el-table-column label="付款人信息" align="center" prop="payerInfo" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="biz_pay_withdrawal_state" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="打款确认" placement="top">
              <el-button link type="primary" icon="Edit" @click="handlePayConfirm(scope.row)"
                         v-hasPermi="['retail:withdrawalRecord:edit']" v-if="scope.row.status==0"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改提现记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-card>
        <el-row :gutter="10" class="mb8">
          <el-col :span="24" :xs="24">
            <el-descriptions
              class="margin-top"
              :column="1"
              border
            >
              <el-descriptions-item label-class-name="my-label">
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <user />
                    </el-icon>
                    收款方信息
                  </div>
                </template>
                {{ selectedRow.payeeInfo }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <pear />
                    </el-icon>
                    付款人信息
                  </div>
                </template>
                {{ selectedRow.payerInfo }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <iphone />
                    </el-icon>
                    谷歌验证码
                  </div>
                </template>
                <el-input v-model="googleAuthCode" placeholder="请输入谷歌驗證碼" clearable style="width: 240px" />
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <monitor />
                    </el-icon>
                    核验结果
                  </div>
                </template>
                <el-radio-group v-model="checkResult" class="ml-4">
                  <el-radio value="1" :label="1" size="large">已打款</el-radio>
                  <el-radio value="2" :label="2" size="large">打款失败</el-radio>
                </el-radio-group>
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
        </el-row>
      </el-card>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="doWithdraw">确认提交</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WithdrawalRecord" lang="ts">
import { listWithdrawalRecord, checkWithdrawalRecord } from '@/api/retail/withdrawalRecord';
import { WithdrawalRecordVO, WithdrawalRecordQuery, WithdrawalRecordForm } from '@/api/retail/withdrawalRecord/types';
import {Iphone, Monitor} from "@element-plus/icons-vue";
import {checkRechargeRecord} from "@/api/retail/rechargeRecord";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { biz_pay_withdrawal_state } = toRefs<any>(proxy?.useDict('biz_pay_withdrawal_state'));

const withdrawalRecordList = ref<WithdrawalRecordVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const withdrawalRecordFormRef = ref<ElFormInstance>();
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const googleAuthCode = ref('');
const checkResult = ref(1);
const selectedRow = ref<WithdrawalRecordVO>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: '提现核验'
});

const initFormData: WithdrawalRecordForm = {
  withdrawalNum: undefined,
  withdrawalType: undefined,
  userId: undefined,
  userName: undefined,
  accountId: undefined,
  amount: undefined,
  payeeInfo: undefined,
  payerInfo: undefined,
  status: undefined,
}
const data = reactive<PageData<WithdrawalRecordForm, WithdrawalRecordQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    withdrawalNum: undefined,
    withdrawalType: undefined,
    userId: undefined,
    userName: undefined,
    accountId: undefined,
    amount: undefined,
    payeeInfo: undefined,
    payerInfo: undefined,
    status: undefined,
    params: {
    }
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询提现记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listWithdrawalRecord(proxy?.addDateRange(queryParams.value, dateRange.value));
  withdrawalRecordList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  withdrawalRecordFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('retail/withdrawalRecord/export', {
    ...queryParams.value
  }, `withdrawalRecord_${new Date().getTime()}.xlsx`)
}
/** 付款确认 */
const handlePayConfirm = (row?:WithdrawalRecordVO) => {
  selectedRow.value = row;
  dialog.visible = true;
}

const doWithdraw = async () => {
  const res = await checkWithdrawalRecord(selectedRow.value?.id, checkResult.value, googleAuthCode.value);
  dialog.visible = false;
  proxy?.$modal.msgSuccess("提现核验完成");
  getList();
}

const doPayConfirm = () => {

}
onMounted(() => {
  getList();
});
</script>
